package cn.community.care.module.system.controller.admin.status;

import cn.community.care.module.system.controller.admin.status.vo.StatusPageReqVO;
import cn.community.care.module.system.controller.admin.status.vo.StatusRespVO;
import cn.community.care.module.system.controller.admin.status.vo.StatusSaveReqVO;
import cn.community.care.module.system.dal.dataobject.status.StatusDO;
import cn.community.care.module.system.service.status.StatusService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 健康状态")
@RestController
@RequestMapping("/health/status")
@Validated
public class StatusController {

    @Resource
    private StatusService statusService;

    @PostMapping("/create")
    @Operation(summary = "创建健康状态")
    public CommonResult<Long> createStatus(@Valid @RequestBody StatusSaveReqVO createReqVO) {
        return success(statusService.createStatus(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新健康状态")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody StatusSaveReqVO updateReqVO) {
        statusService.updateStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除健康状态")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStatus(@RequestParam("id") Long id) {
        statusService.deleteStatus(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得健康状态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StatusRespVO> getStatus(@RequestParam("id") Long id) {
        StatusDO status = statusService.getStatus(id);
        return success(BeanUtils.toBean(status, StatusRespVO.class));
    }

    @GetMapping("/get/by/userID")
    @Operation(summary = "获得健康状态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StatusRespVO> getStatusByUserID(@RequestParam("id") Long id) {
        StatusDO status = statusService.getStatusByUserID(id);
        return success(BeanUtils.toBean(status, StatusRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得健康状态分页")
    public CommonResult<PageResult<StatusPageReqVO>> getStatusPage(@Valid StatusPageReqVO pageReqVO) {
        PageResult<StatusPageReqVO> pageResult = statusService.getStatusPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出健康状态 Excel")
    @OperateLog(type = EXPORT)
    public void exportStatusExcel(@Valid StatusPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StatusPageReqVO> list = statusService.getStatusPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "健康状态.xls", "数据", StatusRespVO.class,
                        BeanUtils.toBean(list, StatusRespVO.class));
    }

}